package api

import (
	"github.com/gin-gonic/gin"
	"go-learn/dao"
	"go-learn/model/request"
	"go-learn/model/response"
)

// AddScore 添加成绩
func AddScore(c *gin.Context) {
	var data request.Players
	_ = c.ShouldBind(&data)
	err := dao.AddPlayer(data.Players)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	response.Ok(c)
}

// 获取排行榜并分页
func GetRankList(c *gin.Context) {
	var data request.Query
	_ = c.ShouldBind(&data)
	page, err := dao.GetRankList(data.Page, data.Size)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	response.OkWithData(page, c)
}

// 获取分数区间排行榜
func GetRankListByScore(c *gin.Context) {
	var data request.Query
	_ = c.ShouldBind(&data)
	page, err := dao.GetRankListByScore(data.Min, data.Max, data.Page, data.Size)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	response.OkWithData(page, c)
}

// 根据姓名获取排名
func GetRankByName(c *gin.Context) {
	var data request.Query
	_ = c.ShouldBind(&data)
	rank, err := dao.GetRankByName(data.Name)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	response.OkWithData(rank, c)
}
